home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group01b.txt / 000164_icon-group-sender_Thu Nov 8 12:45:09 2001.msg < prev    next >
Internet Message Format  |  2002-01-03  |  3KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.11.1/8.11.1) id fA8JgQI28608
  4.     for icon-group-addresses; Thu, 8 Nov 2001 12:42:26 -0700 (MST)
  5. Message-Id: <200111081942.fA8JgQI28608@baskerville.CS.Arizona.EDU>
  6. From: Steve Wampler <swampler@noao.edu>
  7. X-Newsgroups: comp.lang.icon
  8. Subject: mutual evaluation
  9. Date: Thu, 08 Nov 2001 11:08:43 -0700
  10. X-Complaints-To: abuse@noao.edu
  11. X-Accept-Language: en
  12. To: icon-group@cs.arizona.edu
  13. Errors-To: icon-group-errors@cs.arizona.edu
  14. Status: RO
  15. Content-Length: 2300
  16.  
  17.  
  18. Since this list has been fairly quiet lately, I thought I post
  19. a couple of (very) simple code snippets showing uses of mutual
  20. evaluation.
  21.  
  22. I was asked recently by someone how to determine the size of
  23. a file from within Icon.  It's really easy (with the newer
  24. releases, at least!) - if f is a file, then
  25.  
  26.     where(seek(f,0))-1
  27.  
  28. produces the number of bytes in a file.  [where() and seek() both
  29. operate on a file using the same positioning used in Icon
  30. when operating on strings...  so where(f) returns the position
  31. of the file pointer.  To get the number of bytes *upto* that
  32. position you have to subtract 1.]
  33.  
  34. Ok, but that solution alters the file pointer position within
  35. the file, which may not be desirable.  Here's a simple function
  36. that produces the size of a file while preserving the current
  37. file pointer position...
  38.  
  39.     procedure fSize(f)
  40.         return 2(oldp := where(f), where(seek(f,0))-1, seek(f,oldp))
  41.     end
  42.  
  43. There's nothing fancy about the use of mutual evaluation here, in
  44. fact, a solution without mutual evaluation isn't much more complex:
  45.  
  46.     procedure fSize2(f)
  47.         oldp := where(f)
  48.         sizef := where(seek(f,0))-1
  49.         seek(f, oldp)
  50.         return sizef
  51.     end
  52.  
  53. but you do have to add a second local variable (sizef) to hold the
  54. result of the middle expression in this case.
  55.  
  56. What if you only have the name of the file?  Then:
  57.  
  58.     procedure fileSize(fName)
  59.         return 2(f := open(fName), where(seek(f,0))-1, close(f))
  60.     end
  61.  
  62. works.  Here, you don't care about the file position but you do want
  63. to close the file when done.  Note that if the open(fName) fails,
  64. the procedure call fails as well.  Coding the same thing w/o mutual
  65. evaluation can be done as:
  66.  
  67.     procedure fileSize2(fName)
  68.         if f := open(fName) then {
  69.             sizef := where(seek(f,0))-1
  70.             close(f)
  71.             return sizef
  72.             }
  73.     end
  74.  
  75. The idiom in use in these mutual evaluation examples is simple:
  76.  
  77.    return 2(initialize, compute, cleanup)
  78.  
  79. which is really pretty clean - most of the time.  It's not clean
  80. when any of initialize, compute, or cleanup is complex (but *that*
  81. can often be solved by encapsulating the complex action in a
  82. procedure).
  83.  
  84. What else could be a problem?
  85.  
  86. --
  87. Steve Wampler-  SOLIS Project, National Solar Observatory
  88. swampler@noao.edu
  89.